﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mani.Common.Security;
using RastinArgham.CRM.Business;
using Mani.Common;

namespace RastinArgham.CRM.Web
{
    public partial class list_products : System.Web.UI.Page
    {
        public ISecurityManager securityManager { get; set; }
        public CRMRepository crmRepository { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (securityManager == null)
            {
                securityManager = new SecurityManager();
                securityManager.authProvider = new WebAuthorizationProvider();
            }

            if (crmRepository == null)
                crmRepository = new CRMRepository();


            if (!securityManager.IsActionPermitted("products"))
                Response.Redirect("~/access_denied.aspx");

            if (!Page.IsPostBack)
            {
                LoadProvinces();
                LoadProducts();
            }
        }

        private void LoadProvinces()
        {
            ddlProvinces.DataSource = crmRepository.GetProvinces();
            ddlProvinces.DataBind();
        }

        private void LoadProducts()
        {
            gvProducts.DataSource = crmRepository.GetProducts();
            gvProducts.DataBind();
        }

        protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtId.Text))
                New();
            else
                Update();

            UIHelper.ClientAlert(this, "تغییرات با موفقیت ذخیره شد");
            ClearForm();
            LoadProducts();
        }

        private void New()
        {
            Product p = new Product();
            p.OperatorId = securityManager.authProvider.GetCurrentUser().UserId;
            p.Description = txtDescription.Text;
            p.Enable = cbEnable.Checked;
            p.Price = Convert.ToInt64( UIHelper.RemoveFormat(txtPrice.Text) );

            if (ddlProvinces.SelectedValue != "-1")
                p.Province = crmRepository.GetProvinceById(Convert.ToInt32(ddlProvinces.SelectedValue));

            p.TaxPercent = Convert.ToDouble(txtTaxPercent.Text);
            p.ProductName = txtProductName.Text;

            crmRepository.AddProduct(p);
            crmRepository.Save();
        }

        private void Update()
        {
            Product p = crmRepository.GetProductById(Convert.ToInt32(txtId.Text));
            
            p.OperatorId = securityManager.authProvider.GetCurrentUser().UserId;
            p.Description = txtDescription.Text;
            p.Enable = cbEnable.Checked;
            p.Price = Convert.ToInt64(UIHelper.RemoveFormat(txtPrice.Text));

            if (ddlProvinces.SelectedValue == "-1")
            {
                p.Province = null;
                p.ProvinceId = null;
            }
            else
            {
                p.Province = crmRepository.GetProvinceById(Convert.ToInt32(ddlProvinces.SelectedValue));
            }

            p.ProductName = txtProductName.Text;
            p.TaxPercent = Convert.ToDouble(txtTaxPercent.Text);

            crmRepository.Save();
        }

        protected void gvProducts_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            ClearForm();

            int productId = Convert.ToInt32(gvProducts.DataKeys[e.NewSelectedIndex].Value);
            Product product = crmRepository.GetProductById(productId);
            txtId.Text = product.ProductId.ToString();
            txtProductName.Text = product.ProductName;
            txtDescription.Text = product.Description;
            txtPrice.Text = UIHelper.FormatString(product.Price);
            txtTaxPercent.Text = product.TaxPercent.ToString();
            cbEnable.Checked = product.Enable;

            if (product.ProvinceId.HasValue)
                ddlProvinces.Items.FindByValue(product.ProvinceId.Value.ToString()).Selected = true;
        }

        private void ClearForm()
        {
            txtId.Text = String.Empty;
            txtProductName.Text = String.Empty;
            txtDescription.Text = String.Empty;
            txtPrice.Text = String.Empty;
            txtTaxPercent.Text = String.Empty;
            cbEnable.Checked = false;
            ddlProvinces.ClearSelection();
        }
    }
}